package gui;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import sqlstuff.SQLManage;

public class Listener implements TableModelListener{

	private ArrayList<String> Animal = new ArrayList<String>(Arrays.asList("AID","Breed","Purpose","MedStatus","Sex","Age"));
	private ArrayList<String> FedTo = new ArrayList<String>(Arrays.asList("Herd","Feed"));
	private ArrayList<String> Feed = new ArrayList<String>(Arrays.asList("DID","Cost","Product","Amount","Supplier"));
	private ArrayList<String> Field = new ArrayList<String>(Arrays.asList("FID","Crop","Treatment","DateSeeded","Yield","Location"));
	private ArrayList<String> Herd = new ArrayList<String>(Arrays.asList("Manure","Food"));
	private ArrayList<String> Member = new ArrayList<String>(Arrays.asList("Animal","Herd"));
	private ArrayList<String> Pasture = new ArrayList<String>(Arrays.asList("PID","Name","Location","Opened On"));
	private ArrayList<String> Pastured= new ArrayList<String>(Arrays.asList("Herd","Pasture"));
	private ArrayList<String> SupervisedBy = new ArrayList<String>(Arrays.asList("Supervisor","Worker"));
	private ArrayList<String> Worker = new ArrayList<String>(Arrays.asList("WID","Name","Pay","Position","WorkShift"));
	private ArrayList<String> WorksOn = new ArrayList<String>(Arrays.asList("Worker","Field"));
	private ArrayList<String> WorksWith = new ArrayList<String>(Arrays.asList("Worker","Herd"));
	
	private JTable table;
	private SQLManage sql;
	public Listener(JTable table, SQLManage sql){
		this.table = table;
		this.sql = sql;
		//System.out.println("Listener Constructed");
	}
	@Override
	public void tableChanged(TableModelEvent e) {
		System.out.print("Table Changed: ");
		//Identify which table changed (which table this is)
		ArrayList<String> thistable = new ArrayList<String>();
		for(int i=0;i<table.getModel().getColumnCount();i++)
		thistable.add(table.getModel().getColumnName(i));
		if(thistable.equals(Animal)){
			//do stuff for the animal tables
			System.out.println("Animal Table");
			int row = e.getFirstRow();
			String AID =(table.getModel().getValueAt(row, 0)).toString().trim();
			String age = (table.getModel().getValueAt(row, 5)).toString().trim();
			String Breed = table.getModel().getValueAt(row, 1).toString().trim();
			String Purpose = table.getModel().getValueAt(row, 2).toString().trim();
			String MedStatus = table.getModel().getValueAt(row, 3).toString().trim();
			String Sex = table.getModel().getValueAt(row, 4).toString();
			
			//System.out.println("AID:" +AID+ "Age: "+ age);
			String[] args = {"@AID="+AID+",","@Age="+age};
			sql.executeSprocWithParams("Modify Animal Age", args);
			
			String[] args2 ={"@AID="+AID+", @Breed="+Breed};
			sql.executeSprocWithParams("Modify Animal Breed", args2);
			
			String[] args3 = {"@AID="+AID+", @Purpose="+Purpose};
			sql.executeSprocWithParams("Modify Animal Purpose", args3);
			
			String[] args4 = {"@AID="+AID+", @MedStatus="+MedStatus};
			sql.executeSprocWithParams("Modify Animal MedStatus", args4);
			
			String[] args5 = {"@AID="+AID+", @Sex="+Sex};
			sql.executeSprocWithParams("Modify Animal Sex", args5);
		}
		else if(thistable.equals(Field)){
			//do stuff for field
			System.out.println("Field Table");
			int row = e.getFirstRow();
			String FID =(table.getModel().getValueAt(row, 0)).toString().trim();
			String Crop = (table.getModel().getValueAt(row, 1)).toString().trim();
			String Treatment = table.getModel().getValueAt(row, 2).toString().trim();
			String DateSeeded = table.getModel().getValueAt(row, 3).toString().trim();
			String Yield = table.getModel().getValueAt(row, 4).toString().trim();
			String Location = table.getModel().getValueAt(row, 5).toString().trim();
			
			String[] args = {"@FID="+FID+",","@Crop="+Crop};
			sql.executeSprocWithParams("Modify Field Crop", args);
			
			String[] args2 ={"@FID="+FID+", @DateSeeded="+DateSeeded};
			sql.executeSprocWithParams("Modify Field DateSeeded", args2);
			
			String[] args3 = {"@FID="+FID+", @Treatment="+Treatment};
			sql.executeSprocWithParams("Modify Field Treatment", args3);
			
			String[] args4 = {"@FID="+FID+", @Yield="+Yield};
			sql.executeSprocWithParams("Modify Field Yield", args4);
			
			String[] args5 = {"@FID="+FID+", @Location="+Location};
			sql.executeSprocWithParams("Modify Field Location", args5);
		}
		else if(thistable.equals(Pasture)){
			//do stuff for Pasture
			System.out.println("Pasture Table");
			int row = e.getFirstRow();
			String PID =(table.getModel().getValueAt(row, 0)).toString().trim();
			String DateOpened = (table.getModel().getValueAt(row, 1)).toString().trim();
			String Location = table.getModel().getValueAt(row, 2).toString().trim();
			String Name= table.getModel().getValueAt(row, 3).toString().trim();
			
			String[] args = {"@PID="+PID+",","@DateOpened="+DateOpened};
			sql.executeSprocWithParams("Modify Pasture DateOpened", args);
			
			String[] args2 ={"@PID="+PID+", @Location="+Location};
			sql.executeSprocWithParams("Modify Pasture Location", args2);
			
			String[] args3 = {"@PID="+PID+", @Name="+Name};
			sql.executeSprocWithParams("Modify Pasture Name", args3);
		}
		else if(thistable.equals(Worker)){
			//do stuff for Worker
			System.out.println("Worker Table");
			int row = e.getFirstRow();
			String WID =(table.getModel().getValueAt(row, 0)).toString().trim();
			String Pay = (table.getModel().getValueAt(row, 1)).toString().trim();
			String Name= table.getModel().getValueAt(row, 2).toString().trim();
			String Position= table.getModel().getValueAt(row, 3).toString().trim();
			String WorkShift= table.getModel().getValueAt(row, 4).toString().trim();
			
			String[] args = {"@WID="+WID+",","@Pay="+Pay};
			sql.executeSprocWithParams("Modify Worker Pay", args);
			
			String[] args2 ={"@WID="+WID+", @Name="+Name};
			sql.executeSprocWithParams("Modify Worker Name", args2);
			
			String[] args3 = {"@WID="+WID+", @Position="+Position};
			sql.executeSprocWithParams("Modify Worker Position", args3);
						
			String[] args5 = {"@WID="+WID+", @WorkShift="+WorkShift};
			sql.executeSprocWithParams("Modify Worker WorkShift", args5);
		}
		else{
			//OH DAMN SON YOU MESSED UP
			System.err.println("\nERROR: Doesn't match any of the tables:");
			System.out.println("       "+thistable.toString());
		}
	}

}
